﻿@using StackExchange.Opserver.Data.Redis;
@model List<RedisInstanceOperation>
@{ 
    var operations = Model;
}
@helper RenderTolopogy(RedisInstance top, RedisInstance newMaster, InstanceCommandType command, RedisInstance controlling = null)
{
    if (top == null)
    {
        return;
    }
    <li>
        @if (top == newMaster && command == InstanceCommandType.MakeMaster)
        {
            <strong class="text-primary">@top.IconSpan() @top.HostAndPort</strong>
        }
        else if (controlling != null && top.HostAndPort == controlling.HostAndPort)
        {
            <span class="text-primary">@top.IconSpan() @top.HostAndPort</span>
        }
        else if (top.IsMaster)
        {
            <strong>@top.IconSpan() @top.HostAndPort</strong>
        }
        else
        {
            @:@top.IconSpan() @top.HostAndPort
        }
        <span class="text-muted">(@top.Name)</span>
        @if (controlling != null && top == newMaster)
        {
            <ul class="proposed">@RenderTolopogy(controlling, newMaster, command, controlling)</ul>
        }
        @if (top.SlaveConnections?.Any() ?? false)
        {
            <ul>
                @foreach (var sc in top.SlaveConnections)
                {
                    var slave = sc.GetServer();
                    if (controlling != null && slave.HostAndPort == controlling.HostAndPort) { continue; }
                    @RenderTolopogy(slave, newMaster, command, controlling)
                }
            </ul>
        }
    </li>
}
<div class="list-group list-group-condensed js-redis-server-actions-preview">
    <div class="row">
        @if (operations.Count == 0)
        {
            <span class="col-md-12 text-muted">No operations selected.</span>
        }
        @foreach (var op in operations)
        {
            <input type="hidden" name="operations" value="@op.ToString()" />
            var i = op.Instance;
            if (op.Command == InstanceCommandType.MakeMaster)
            {
                <div class="col-md-12">
                    <div class="well well-sm well-condensed">
                        <ul class="ul-tree">
                            @RenderTolopogy(op.Instance, op.Instance, op.Command)
                        </ul>
                    </div>
                </div>
            }
            else if (op.Command == InstanceCommandType.SlaveTo)
            {
                var newMaster = op.NewMaster;
                <div class="col-md-12">
                    <div class="well well-sm well-condensed">
                        <ul class="ul-tree">
                            @RenderTolopogy(op.NewMaster.TopMaster, op.NewMaster, op.Command, op.Instance)
                        </ul>
                    </div>
                </div>
            }
        }
    </div>
</div>